import 'package:flutter/material.dart';
import 'package:flutter_animate/loading/loging_canvas.dart';

class LoadingAnimateWidget extends StatefulWidget {
  final double width;
  final Widget child;

  const LoadingAnimateWidget({Key? key, required this.width, required this.child}) : super(key: key);

  @override
  _LoadingAnimateWidgetState createState() => _LoadingAnimateWidgetState();
}

class _LoadingAnimateWidgetState extends State<LoadingAnimateWidget> with SingleTickerProviderStateMixin{
  late AnimationController _animationController;

  late Animation<double> _animation;


  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 3),
    );

    _animation = Tween(begin: 0.0, end: 2.0).animate(_animationController);

    _animationController.repeat();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        widget.child,
        AnimatedBuilder(
          animation: _animationController,
          builder: (_, child) {
            return CustomPaint(
              painter: LoadingCanvas(width: widget.width, value: _animation.value),
            );
          },
        )
      ],
    );

  }
}
